﻿using AutoMapper;
using BanquetPortion.Dtos.DataDictionaryDtos;
using BanquetPortion.Dtos.FromDataDictionaryDto;
using BanquetPortion.Models;
using BanquetPortion.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BanquetPortion.Controllers
{
    [Route("api/dataDictionarys")]
    [ApiController]
    public class DataDictionarysController : ControllerBase
    {
        private readonly IDataDictionaryRepository _dataDictionaryRepository;
        private readonly IMapper _mapper;
        public DataDictionarysController(
            IDataDictionaryRepository dataDictionaryRepository,
            IMapper mapper
            )
        {
            _dataDictionaryRepository = dataDictionaryRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取数据字典中所有数据的接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDataDictionaryAsync()
        {
            var dataDictionarysFromRepo = await _dataDictionaryRepository.GetDataDictionarysAsync();
            if (dataDictionarysFromRepo == null || dataDictionarysFromRepo.Count() <= 0)
            {
                return NotFound("还未存入数据");
            }
            var dataDictionarysDto = _mapper.Map<IEnumerable<DataDictionaryDto>>(dataDictionarysFromRepo);
            return Ok(dataDictionarysDto);
        }
        /// <summary>
        /// 根据数据字典 ID 获取数据字典中的单挑数据
        /// </summary>
        /// <param name="dataDictionaryId"></param>
        /// <returns></returns>
        [HttpGet("{dataDictionaryId}", Name = "GetDataDictionaryById")]
        public async Task<IActionResult> GetDataDictionaryAsync(int dataDictionaryId)
        {
            var dataDictionaryFromRepo = await _dataDictionaryRepository.GetDataDictionaryByIdAsync(dataDictionaryId);
            if (dataDictionaryFromRepo == null)
            {
                return NotFound($"数据 {dataDictionaryId} 不存在");
            }
            var dataDictionaryDto = _mapper.Map<DataDictionaryDto>(dataDictionaryFromRepo);
            return Ok(dataDictionaryDto);
        }
        [HttpPost]
        public async Task<IActionResult> CreateDataDictionaryAsync([FromBody] DataDictionaryForCreateDto dataDictionaryForCreateDto)
        {
            var dataDictionaryModel = _mapper.Map<DataDictionary>(dataDictionaryForCreateDto);
            _dataDictionaryRepository.CreateDataDictionary(dataDictionaryModel);
            await _dataDictionaryRepository.SaveAsync();
            // 把数据库中新建的数据 Map 出来，变成 DTO 后传回给 API 作为数据输出
            var dataDictionaryToRoute = _mapper.Map<DataDictionaryDto>(dataDictionaryModel);
            return CreatedAtRoute("GetDataDictionaryById", new { datadictionaryid = dataDictionaryToRoute.ParentId }, dataDictionaryToRoute);
        }
        /// <summary>
        /// 更改数据字典
        /// </summary>
        /// <param name="dataDictionaryId"></param>
        /// <param name="dataDictionaryForUpDateDto"></param>
        /// <returns></returns>
        [HttpPut("{dataDictionaryId}")]
        public async Task<IActionResult> UpDataDataDictionaryAsync(
            [FromRoute] int dataDictionaryId,
            [FromBody] DataDictionaryForUpDateDto dataDictionaryForUpDateDto)
        {
            if (!await _dataDictionaryRepository.DataDictionaryExistsAsync(dataDictionaryId))
            {
                return NotFound($"未找到资源：{dataDictionaryId}");
            }
            var dataDictionaryFromRepo = await _dataDictionaryRepository.GetDataDictionaryByIdAsync(dataDictionaryId);
            _mapper.Map(dataDictionaryForUpDateDto, dataDictionaryFromRepo);
            await _dataDictionaryRepository.SaveAsync();
            var getDataDictionary = await _dataDictionaryRepository.GetDataDictionaryByIdAsync(dataDictionaryId);
            var dataDictionaryDto = _mapper.Map<DataDictionaryDto>(getDataDictionary);
            return Ok(dataDictionaryDto);
        }
        [HttpDelete("{dataDictionaryId}")]
        public async Task<IActionResult> DeleteDataDictionaryAsync([FromRoute] int dataDictionaryId)
        {
            if (!await _dataDictionaryRepository.DataDictionaryExistsAsync(dataDictionaryId))
            {
                return NotFound($"未找到资源：{dataDictionaryId}");
            }
            var dataDictionary = await _dataDictionaryRepository.GetDataDictionaryByIdAsync(dataDictionaryId);
            _dataDictionaryRepository.DeleteDataDictionary(dataDictionary);
            await _dataDictionaryRepository.SaveAsync();
            return NoContent();
        }
    }
}
